package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.constants.Common;
import com.itany.exception.UpdateFlagException;
import com.itany.pojo.Book;
import com.itany.pojo.Examine;
import com.itany.pojo.ManagerUser;
import com.itany.service.ExamineService;
import com.itany.vo.SupplierExamine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Author：sl
 * Date：2019-09-18 下午1:57
 * Description：<描述>
 */
@Controller
@RequestMapping("/examine")
public class ExamineController {

    @Autowired
    private ExamineService examineService;

    @RequestMapping("/addExamine")
    public ModelAndView addExamine(Book book, @RequestParam("file") CommonsMultipartFile file, HttpSession session) {
        ModelAndView mv = new ModelAndView();


        ManagerUser managerUser = (ManagerUser) session.getAttribute("managerUser");


        Examine examine = new Examine();
        examine.setNumber(book.getNumber());
        examine.setSupplierId(managerUser.getSupplierId());


        //文件上传
        try {
            //1、获取文件名称
            String fileName = file.getOriginalFilename();
            //2、生成新的文件名
            String uuid = UUID.randomUUID().toString();
            //3、新的文件名 = 生成的新文件名+文件后缀
            String newName = uuid + fileName.substring(fileName.lastIndexOf("."));
            //4、上传到一个本地的目录下
            String pathName = "/home/soft01/upload/" + newName;
            file.transferTo(new File(pathName));

            //将图片的路径保存倒数据库
            book.setImgUrl(pathName);


            examineService.addExamine(book, examine);
            mv.setViewName("backend/supplier_examine");

        } catch (IOException e) {
            mv.addObject("msg", "文件上传失败");
            mv.setViewName("backend/supplier_examine");
        }

        return mv;
    }


    @ResponseBody
    @RequestMapping("/findAllExamineByCondition")
    public Map<String, Object> findAllExamineByCondition(SupplierExamine supplierExamine, @RequestParam(value = "page", required = false, defaultValue = "1") Integer page, @RequestParam(value = "rows", required = false, defaultValue = "2") Integer rows, HttpSession session) {
        Map<String, Object> map = new HashMap<>();

        System.out.println(supplierExamine);
        ManagerUser managerUser = (ManagerUser) session.getAttribute("managerUser");

        if (null != managerUser.getSupplierId()) {

            supplierExamine.setSupplierId(managerUser.getSupplierId());
        }


        PageInfo<Examine> examinePageInfo = examineService.findAllExamineByCondition(supplierExamine, page, rows);
        map.put("total", examinePageInfo.getTotal());
        map.put("rows", examinePageInfo.getList());
        return map;
    }


    @RequestMapping("/modifyFlag")
    public ModelAndView modifyFlag(Examine examine) {
        System.out.println(examine);
        ModelAndView mv = new ModelAndView();
        System.out.println(examine);
        try {

            if (Common.ExamineCommons.EXAMINE_SDISABLE == examine.getFlag()) {
                //审核不通过
                examineService.disableFlag(examine);
            } else {
                //审核通过
                examineService.enableFlag(examine);
                //不能审核通过禁用状态的书籍
            }
        } catch (UpdateFlagException e) {
            mv.addObject("msg", e.getMessage());
            e.printStackTrace();
        }catch (Exception e){
            mv.addObject("msg", e.getMessage());
            e.printStackTrace();
        }
        mv.setViewName("backend/supplier_platform");
        return mv;
    }

}
